local vscode = require "vscode-neovim"
local api = require "vscode-neovim.api"
local function which(program)
  local handle = io.popen("which " .. program)
  if handle then
    local result = handle:read "*a"
    handle:close()
    return result:sub(1, -2) -- 移除末尾的换行符
  end
  return nil
end

vscode.update_config(
  "editor.fontFamily",
  "Menlo,'MesloLGS Nerd Font Mono','MonaspiceNe Nerd Font Mono',Consolas,Monaco,'Courier New',monospace,'CaskaydiaCove Nerd Font Mono'",
  "global"
)
vscode.update_config("editor.guides.bracketPairs", "active", "global")
vscode.update_config("editor.fontSize", 14, "global")
vscode.update_config("editor.lineNumbers", "relative", "global")
vscode.update_config("editor.formatOnSave", true, "global")
vscode.update_config("code-runner.clearPreviousOutput", true, "global")
vscode.update_config("code-runner.respectShebang", false, "global")
vscode.update_config("code-runner.saveFileBeforeRun", true, "global")
-- vscode.update_config("code-runner.executorMap", { ["python"] = "$pythonPath $fullFileName" },"global")
vscode.update_config("editor.formatOnSave", true, "global")
vscode.update_config("ruff.lint.enable", false, "global")
vscode.update_config("ruff.organizeImports", false, "global")
vscode.update_config("terminal.integrated.fontSize", 13, "global")
vscode.update_config("editor.minimap.autohide", true, "global")
vscode.update_config("editor.cursorSurroundingLines", 5, "global")
vscode.update_config("python.analysis.autoImportCompletions", true, "global")
vscode.update_config("python.analysis.completeFunctionParens", true, "global")
vscode.update_config("python.terminal.activateEnvironment", false, "global")
vscode.update_config("git.autofetch", true, "global")
vscode.update_config("git.confirmSync", false, "global")
vscode.update_config("stylua.searchBinaryInPATH", true, "global")
vscode.update_config("stylua.disableVersionCheck", true, "global")
-- vscode.update_config("selene.run", "onIdle", "global")
vscode.update_config("extensions.autoCheckUpdates", false, "global")
vscode.update_config("extensions.autoUpdate", "onlySelectedExtensions", "global")
vscode.update_config("workbench.layoutControl.enabled", false, "global")
-- vscode.update_config("workbench.activityBar.location", "hidden", "global")
vscode.update_config("window.commandCenter", false, "global")
vscode.update_config("update.mode", "manual", "global")

local js_conf = {}
js_conf["editor.defaultFormatter"] = "esbenp.prettier-vscode"
api.call("update_config", { args = { "[javascript]", js_conf, "global" } })
api.call("update_config", { args = { "[typescript]", js_conf, "global" } })
api.call("update_config", { args = { "[jsonc]", js_conf, "global" } })
api.call("update_config", { args = { "[json]", js_conf, "global" } })
api.call("update_config", { args = { "[lua]", { ["editor.defaultFormatter"] = "JohnnyMorganz.stylua" }, "global" } })
api.call("update_config", {
  args = {
    "[python]",
    {
      ["editor.codeActionsOnSave"] = { ["source.organizeImports"] = "explicit" },
      ["editor.defaultFormatter"] = "charliermarsh.ruff",
    },
    "global",
  },
})

api.call("update_config", {
  args = {
    "[markdown]",
    { ["editor.wordWrap"] = "off", ["editor.defaultFormatter"] = "esbenp.prettier-vscode" },
    "global",
  },
})
api.call(
  "update_config",
  { args = { "code-runner.executorMap", { ["python"] = "$pythonPath $fullFileName" }, "global" } }
)
api.call("update_config", { args = { "files.associations", { ["*.json"] = "jsonc" }, "global" } })
api.call("update_config", { args = { "codeium.enableConfig", { ["*"] = true, ["markdown"] = true }, "global" } })

if vim.loop.os_uname().sysname == "Windows_NT" then
  local bin_path = vim.fn.stdpath "data" .. "\\mason\\bin\\"
  vscode.update_config("selene.selenePath", bin_path .. "selene.cmd", "global")
  vscode.update_config("clangd.path", bin_path .. "clangd.cmd", "global")
  vscode.update_config("shfmt.executablePath", bin_path .. "shfmt.cmd", "global")
  vscode.update_config("stylua.styluaPath", bin_path .. "stylua.cmd", "global")
  vscode.update_config("bashIde.shellcheckPath", bin_path .. "shellcheck.cmd", "global")
else
  local bin_path = vim.fn.stdpath "data" .. "/mason/bin/"
  vscode.update_config("selene.selenePath", bin_path .. "selene", "global")
  if vim.loop.os_uname().sysname == "Linux" and vim.loop.os_uname().machine == "aarch64" then
    vscode.update_config("clangd.path", which "clangd", "global")
  else
    vscode.update_config("clangd.path", bin_path .. "clangd", "global")
  end
  vscode.update_config("shfmt.executablePath", bin_path .. "shfmt", "global")
  vscode.update_config("stylua.styluaPath", bin_path .. "stylua", "global")
  vscode.update_config("bashIde.shellcheckPath", bin_path .. "shellcheck", "global")
end

if vim.loop.os_uname().sysname == "Linux" then
  vscode.update_config("window.titleBarStyle", "custom", "global")
  vscode.update_config("keyboard.dispatch", "keyCode", "global")
end

if vim.loop.os_uname().sysname == "Windows_NT" then
  vscode.update_config("terminal.integrated.defaultProfile.windows", "Git Bash", "global")
end
